$(function(){
	//
	$("#addSubject").click(function(){
		$("#showAddSubjectModal").modal("show");
	});
	
	
	
	//
	$(".editSubject").click(function(){
		var subId = $(this).parent().siblings().eq(0).text();
		$.ajax({
			type: 'POST',
			url: 'subjecEdit',
			data:{
				"subId":subId
				},
			dataType: "json",
			success:function(date){
				 $(".subId").val(date.subId);
				 $(".subName").val(date.subName);
				 $(".description").val(date.description);
				 $(".testCount").val(date.testCount);
				 $(".totalTime").val(date.totalTime);
				 $(".totalScore").val(date.totalScore);
				 if(date.status == 1){
					 $("input[name='status']").prop("checked",true);
				 }else if(date.status == 0){
					 $("input[name='status']").prop("checked",false);
				 }
			}
		});
		$("#showEditSubjectModal").modal("show");
	});
	
	
	//
	$(".adminStudent").click(function(){
		var subId = $(this).parent().siblings().eq(0).text();
		$(".hqSubId").val(subId);
		$.ajax({
			type: 'POST',
			url: 'allStudent',
			data:{
				"subId":subId
				},
			dataType: "json",
			success:function(data){
				var htmlString = " ";
				var selectedUser = data.selectedUser;
				var user = data.allUser[i];
				//得到所以这个科目关联的学生
				for(var i=0;i<data.allUser.length;i++){
					var user = data.allUser[i];
					var checked = checkSelectedUser(selectedUser, user.userId) ? 'checked="checked"':'';
					htmlString += '<div class="checkbox">';
					htmlString += '		<label>';
					htmlString += '			<input name="subjectStudents" class="ace ace-checkbox-2" type="checkbox" value="'
											+ user.userId +'" '+checked+' />';
					htmlString += '    		<span class="lbl">'+user.userName+'</span>';
					htmlString += '    </label>';
					htmlString += '</div>';
				}
				$("#subjectStudentContent").html(htmlString);
				$("#hqSubId").val(subId);
			}
		});
		$("#modelSubjectName").text($(this).parent().siblings().eq(1).text());
		$("#teacherManageStudent").modal("show");
	});
	
	function checkSelectedUser(selectedUser, userId){
		for(var i=0;i<selectedUser.length;i++){
			if(selectedUser[i]==userId){
				return true;
				break;
			}
		}
	}
	
	
	
	$(".checkId").click(function(){
		var val = $(this).val();
	});
	
	$(".teacherManage").click(function(){
		var subjectStudent = [];
		//获取当前已经勾上的学生的学号
		for(var i=0;i<$('input[name="subjectStudents"]:checked').length;i++){
			subjectStudent[i] = $($('input[name="subjectStudents"]:checked')[i]).val();
		}
		$.ajax({
			type: 'POST',
			url: 'saveUserSubject',
			data: {
				subjectStudent: subjectStudent,
				subId: $("#hqSubId").val()
			},
			dataType: "json",
			success: function(data){
				//判断是否成功
				if(data.success){
					alert('成功！');
					$("#teacherManageStudent").modal("hide");
					
				}else{
					alert('失败！');
				}
			}
		});
	});
	
	
	$("#addSubjectModal").click(function(){
		var subName = $("#subName").val();
		var description = $("#description").val();
		var testCount = $("#testCount").val();
		var totalTime = $("#totalTime").val();
		var totalScore = $("#totalScore").val();
		var status;
		if ($('#status').is(':checked')) {
			status = 1;
		}else{
			status = 0;
		}
		
		
		
		 if(subName!=""){
			 if(description!=""){
				 if(testCount!=""){
					 if(totalTime!=""){
						 if(totalScore!=""){
							 $.ajax({
									type: 'POST',
									url: 'subjecAdd',
									data:{
										"subName":subName,
										"description":description,
										"testCount":testCount,
										"totalTime":totalTime,
										"totalScore":totalScore,
										"status":status
										},
									dataType: "json",
									success:function(date){
										 alert(date.massage);
										 location.reload();
									}
								});
						 }else{
							 alert("总分不能为空");
						 }
					 }else{
						 alert("考试总时长不能为空");
					 }
				 }else{
					 alert("科目题数不能为空");
				 }
			 }else{
				 alert("科目描述不能为空");
			 } 
		 }else{
			 alert("科目名不能为空！");
		 }
	});
	
	$(".boxStatus").click(function(){
		var subId = $(this).parent().siblings().eq(0).text();
		var status;
		if($(this).prop("checked")){
			status = 1;
		}else{
			status = 0;
		}
		$.ajax({
			type: 'POST',
			url: 'editStatus',
			data:{
				"subId":subId,
				"status":status
				},
			dataType: "json",
			success:function(date){
				 alert(date.massage);
				 location.reload();
			}
		});
	});
	
	$(".editSubjectBtn").click(function(){
		var subId = $(".subId").val();
		var subName = $(".subName").val();
		var description = $(".description").val();
		var testCount = $(".testCount").val();
		var totalTime = $(".totalTime").val();
		var totalScore = $(".totalScore").val();
		var status;
		 if($("input[name='status']").prop("checked")){
			 status=1;
		 }else{
			 status=0;
		 }
		 if(subName!=""){
			 if(description!=""){
				 if(testCount!=""){
					 if(totalTime!=""){
						 if(totalScore!=""){
							 $.ajax({
									type: 'POST',
									url: 'editSubject',
									data:{
										"subId":subId,
										"subName":subName,
										"description":description,
										"testCount":testCount,
										"totalTime":totalTime,
										"totalScore":totalScore,
										"status":status
										},
									dataType: "json",
									success:function(date){
										 alert(date.massage);
										 location.reload();
									}
								});
						 }else{
							 alert("总分不能为空");
						 }
					 }else{
						 alert("考试总时长不能为空");
					 }
				 }else{
					 alert("科目题数不能为空");
				 }
			 }else{
				 alert("科目描述不能为空");
			 } 
		 }else{
			 alert("科目名不能为空！");
		 }
	});
});